<?php

namespace frontend\modules\goods\models;

use backend\modules\goods\models\Attribute;
use backend\modules\goods\models\Attrvalue;
use backend\modules\goods\models\AttrvalueGoods;
use backend\modules\goods\models\GoodsDescImages;
use backend\modules\goods\models\GoodsGallImages;
use yii\redis\Cache;
use Yii;

/**
 * This is the model class for table "goods".
 *
 * @property string $goods_id
 * @property string $goods_name
 * @property string $shop_price
 * @property string $market_price
 * @property string $goods_image
 * @property string $gall_images_id
 * @property string $desc_images_id
 * @property integer $is_new
 * @property integer $is_hot
 * @property integer $is_good
 * @property integer $is_promote
 * @property integer $is_delete
 * @property string $cate_id
 * @property string $goods_desc
 * @property integer $is_single
 * @property string $attr_cat_id
 * @property string $goods_number
 * @property string $is_gift
 * @property string $attrText
 * @property string $weight
 * @property string $desc_text
 * @property string $desc_slt
 * @property string $thumb_img
 *
 *
 * @property GoodsDescImages[] $goodsDescImages
 * @property GoodsGallImages[] $goodsGallImages
 */
class Goods extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'goods';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['goods_id'], 'required'],
            [['shop_price', 'market_price','weight'], 'number'],
            [['is_new','is_gift', 'is_hot', 'is_good', 'is_promote', 'is_delete', 'is_single','goods_number'], 'integer'],
            [['goods_desc','attrText','desc_text','desc_slt'], 'string'],
            [['goods_id', 'gall_images_id', 'desc_images_id', 'cate_id', 'attr_cat_id'], 'string', 'max' => 40],
            [['goods_name'], 'string', 'max' => 60],
            [['goods_image','thumb_img'], 'string', 'max' => 255],
            [['goods_name'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'goods_id' => Yii::t('app', 'Goods ID'),
            'goods_name' => Yii::t('app', 'Goods Name'),
            'shop_price' => Yii::t('app', 'Shop Price'),
            'market_price' => Yii::t('app', 'Marker Price'),
            'goods_image' => Yii::t('app', 'Goods Image'),
            'gall_images_id' => Yii::t('app', 'Gall Images ID'),
            'desc_images_id' => Yii::t('app', 'Desc Images ID'),
            'is_new' => Yii::t('app', 'Is New'),
            'is_hot' => Yii::t('app', 'Is Hot'),
            'is_good' => Yii::t('app', 'Is Good'),
            'is_promote' => Yii::t('app', 'Is Promote'),
            'is_delete' => Yii::t('app', 'Is Delete'),
            'cate_id' => Yii::t('app', 'Cate ID'),
            'goods_desc' => Yii::t('app', 'Goods Desc'),
            'is_single' => Yii::t('app', 'Is Single'),
            'attr_cat_id' => Yii::t('app', 'Attr Cat ID'),
            'goods_number' => Yii::t('app', '库存'),
            'is_gift'=> Yii::t('app', '库存'),
            'attrText'=>Yii::t('app', '属性text'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGoodsDescImages()
    {
        return $this->hasMany(GoodsDescImages::className(), ['goods_id' => 'goods_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGoodsGallImages()
    {
        return $this->hasMany(GoodsGallImages::className(), ['goods_id' => 'goods_id']);
    }

    /**
     * @inheritdoc
     * @return \frontend\modules\goods\models\query\GoodsQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \frontend\modules\goods\models\query\GoodsQuery(get_called_class());
    }

    /**
     * 获取属性分类缺口
     *
     * @param $attr_cat_id
     * @return array
     */
    public static function getArbByArcandgoods($attr_cat_id,$goods_id)
    {
        $attribute = Attribute::find()->where(['attr_cat_id'=>$attr_cat_id])->asArray()->all();
        $attrvalueARR = array();
        foreach($attribute as $k=>$v) {
            $attr_id = $v['attr_id'];
            $Attrvalue = Attrvalue::find()->where(['attr_id'=>$attr_id])->asArray()->all();
            foreach($Attrvalue as $key=>$value){
                $AttrvalueGoods = AttrvalueGoods::find()->where(['attr_value_id'=>$value['attr_value_id'],'goods_id'=>$goods_id])->one();
                if($AttrvalueGoods){
                    $Attrvalue[$key]['check'] = 1;
                }
            }
            $v['children'] = $Attrvalue;
            $attrvalueARR[] = $v;
        }

        return $attrvalueARR;
    }

    /**
     * 商品信息，商品属性
     *
     * @return \api\modules\goods\models\Goods[]|array
     */
    public static function getGoodslistInfo()
    {
        //缓存
        $cache = new Cache();
        $goodsAttributeInfo = $cache->get('goodsAttributeInfo');
        if($goodsAttributeInfo){
            return $goodsAttributeInfo;
        }

        $goodsList = Goods::find()->where(['is_delete'=>0])->asArray()->all();

        foreach($goodsList as $key=>$val) {
            $goodsList[$key] = static::getGoodsInfo($val);
        }

        //缓存新数据
        if(!$cache->set('goodsAttributeInfo',$goodsList)){
            return  false;
        }
        return $goodsList;
    }

    /**
     * 获取单个商品信息
     *
     * @param $goods_id
     * @return \api\modules\goods\models\Goods|array|null
     */
    public static function getGoodsInfo($goodsInfo)
    {
        //缓存
        $cache = new Cache();
        $goodsAttributeInfo = $cache->get('sku'.$goodsInfo['goods_id']);
        if($goodsAttributeInfo){
            return $goodsAttributeInfo;
        }

        $goods_id = $goodsInfo['goods_id'];
        $attrvalue = AttrvalueGoods::findAll(['goods_id'=>$goods_id]);
        $goodsGallImages = GoodsGallImages::find()->where(['goods_id'=>$goods_id])->orderBy(['sort_number'=>SORT_ASC])->asArray()->all();

        if($goodsInfo['is_gift']==1) {
            $goodsDescImages = GoodsDescImages::find()->where(['type'=>1])->orderBy(['sort_number'=>SORT_ASC])->asArray()->all();
        } else{
            $goodsDescImages = GoodsDescImages::find()->where(['goods_id'=>$goods_id])->orderBy(['sort_number'=>SORT_ASC])->asArray()->all();
        }
        if($goodsGallImages) {
            $goodsInfo['goodsGallImages'] =$goodsGallImages;
        }
        if($goodsDescImages) {
            $goodsInfo['goodsDescImages'] =$goodsDescImages;
        }
        if($attrvalue) {
            $AttrvalueGoods = static::getArbByArcandgoods($goodsInfo['attr_cat_id'],$goods_id);
            $goodsInfo['attrValueGoods'] =$AttrvalueGoods;
        }


        //缓存新数据
        if(!$cache->set('sku'.$goodsInfo['goods_id'],$goodsInfo)){
            return  false;
        }
        return $goodsInfo;
    }

    /**
     * 获取单个商品信息
     * @param $goodsInfo
     * @return mixed
     */
    public static function getCartByGoodsInfo($goodsInfo)
    {
        $goods_id = $goodsInfo['goods_id'];
        $attrvalue = AttrvalueGoods::findAll(['goods_id'=>$goods_id]);

        $AttrvalueGoods = null;
        if($attrvalue) {
            $AttrvalueGoods = static::getArbByArcandgoods($goodsInfo['attr_cat_id'],$goods_id);
            $goodsInfo['attrValueGoods'] =$AttrvalueGoods;
        }
        $reARR = array();
        if($AttrvalueGoods){
            foreach($AttrvalueGoods as $k=>$v) {
                foreach($v['children'] as $key=>$v) {
                    if(isset($v['check'])&&$v['check']==1){
                        $reARR[] = $v['attr_value_name'];
                    }
                }
            }
        }
        $goodsInfo['attrValueGoods'] = $reARR;
        return $goodsInfo;
    }
}
